package com.wlstock.funi.MissionInstance;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.em.mission.Mission;
import com.em.mission.MissionItem;
import com.em.mission.MissionListener;
import com.em.mission.MissionResultListener;
import com.wlstock.funi.R;
import com.wlstock.funi.constant.URLConstant;
import com.wlstock.funi.request.CustomStringRequest;
import com.wlstock.funi.request.EventBusConstants;
import com.wlstock.funi.request.NetWorkListener;
import com.wlstock.funi.utils.CaculateUtils;
import com.wlstock.funi.utils.NotiIdGenerator;
import com.wlstock.funi.utils.NotiUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by admin on 16/9/10.
 */

public class MoneyFlowStaMission extends MissionItem implements MissionListener, NetWorkListener {


    private static final String MONEY_FLOW = "MONEY_FLOW";
    private CustomStringRequest moneyFlowReq;
    private static MoneyFlowStaMission instance;

    private MoneyFlowStaMission(Context mContext, String name) {
        super(mContext, name);
    }

    public static MoneyFlowStaMission getInstance(Context mContex, String name) {
        if (instance == null) {
            synchronized (MoneyFlowStaMission.class) {
                if (instance == null) {
                    instance = new MoneyFlowStaMission(mContex,name);
                }
            }
        }
        return instance;
    }


    @Override
    public void start(MissionResultListener resultListener) {
        this.resultListener = resultListener;
        start(this, resultListener);
    }

    @Override
    public void beforeExecuteListener(Mission mission) {

    }

    @Override
    public void onExecute(Mission mission) {
        moneyFlowReq();
    }

    @Override
    public void afterExecute(Mission mission) {

    }


    @Override
    public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {

    }

    @Override
    public void onErrorResponse(VolleyError error, String eventCode) {
        if (MONEY_FLOW.equals(eventCode)){
            if (resultListener!=null){
                resultListener.executeFaile(this);
            }
        }

    }

    @Override
    public void onResponse(String response, EventBusConstants eventCode) {

    }

    @Override
    public void onResponse(String response, String eventCode) {
        if (MONEY_FLOW.equals(eventCode)){
            String moneyFlowResult = parseMoneyFlow(response);
            NotiUtils.showSimpleNoti(mContext, NotiIdGenerator.ID_MONEY_FLOW,"资金情况:",moneyFlowResult, R.drawable.ic_market_sta);
        }
    }

    private void moneyFlowReq(){
        if (moneyFlowReq == null){
            this.moneyFlowReq = new CustomStringRequest();
        }
        moneyFlowReq.networkResponseListener = this;
        moneyFlowReq.request(Request.Method.GET, URLConstant.SINA_MONEY,null, null, URLConstant.SINA_MONEY,MONEY_FLOW);
    }

    private String parseMoneyFlow(String response) {
        Pattern compile = Pattern.compile("\\\"(.)*\"");
        Matcher matcher = compile.matcher(response);
        String r1 = "";
        StringBuffer result = new StringBuffer();
        while (matcher.find()){
            r1 = response.substring(matcher.start()+1, matcher.end()-1);
        }
        String[] markets = r1.split(",");
        if (markets!=null && markets.length==3){
            for(int i = 0 ; i < markets.length; i++){
                String[] marketDeatail = markets[i].split("\\|");
                if (marketDeatail!=null && marketDeatail.length==7){
                    try {
                        result.append(marketDeatail[3]).append("资金流入:").append(CaculateUtils.scaleMoneyFlow(Float.valueOf(marketDeatail[1])) +"亿  ");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
        return result.toString();
    }


}
